package com.epalmpay.service.member.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.epalmpay.exception.ApplicationException;
import com.epalmpay.entity.MemberFee;
import com.epalmpay.enumdef.BizEnum;
import com.epalmpay.mapper.MemberFeeMapper;
import org.springframework.stereotype.Service;

import com.epalmpay.dto.member.MemberInDTO;
import com.epalmpay.dto.memberGrade.MemberGradeInDTO;
import com.epalmpay.dto.memberGrade.MemberGradeOutDTO;
import com.epalmpay.dto.shopuser.ShopUserInDTO;
import com.epalmpay.entity.MemberGrade;
import com.epalmpay.mapper.MemberGradeMapper;
import com.epalmpay.service.member.IMemberGradeService;
import com.epalmpay.util.PropertyCopyUtil;

@Service("memberGradeService")
public class MemberGradeServiceImpl implements IMemberGradeService{

	@Resource
	private MemberGradeMapper memberGradeMapper;
	@Resource
	private MemberFeeMapper memberFeeMapper;
	
	
	/**
	 * 会员等级列表
	 */
	@Override
	public List<MemberGradeOutDTO> listMembers(MemberGradeInDTO memberOut) {
		
		return memberGradeMapper.MemberGradeList(memberOut); 
		
	}
	
	/**
	 * 新增会员等级
	 */
	@Override
	public void saveMemberGrade(MemberGradeInDTO memberGradeInDTO) throws Exception {
		MemberGrade memberGrade = new MemberGrade();
		if(memberGradeInDTO.getCanDevelopeMember()==null){
			memberGradeInDTO.setCanDevelopeMember(false);
		}
		if(memberGradeInDTO.getWalletCanConsume()==null){
			memberGradeInDTO.setWalletCanConsume(false);
		}
		if(memberGradeInDTO.getWalletCanReceive()==null){
			memberGradeInDTO.setWalletCanReceive(false);
		}
		if(memberGradeInDTO.getWalletCanRecharge()==null){
			memberGradeInDTO.setWalletCanRecharge(false);
		}
		if(memberGradeInDTO.getWalletCanTransfer()==null){
			memberGradeInDTO.setWalletCanTransfer(false);
		}
		if(memberGradeInDTO.getNeedAudit()==null){
			memberGradeInDTO.setNeedAudit(false);
		}
		memberGradeInDTO.setUpdateAmount(memberGradeInDTO.getUpdateAmount());
		if(memberGradeInDTO.getAttach1()!=null){
			int count = Integer.parseInt(memberGradeInDTO.getAttach1()) * 100;
			memberGradeInDTO.setAttach1(String.valueOf(count));
		}

		PropertyCopyUtil.copyPropertiesExclude(memberGradeInDTO,memberGrade);
		//暂时写死
//		memberGrade.setDiscountRate(1);//折扣比例
		
		memberGrade.setGradeRateId("EC");
		memberGrade.setIcon(memberGradeInDTO.getIcon());
		memberGrade.setGmtCreate(new Date());
		memberGrade.setGmtModified(new Date());
		 memberGradeMapper.insertSelective(memberGrade);
		return;
	}
	
	/**
	 * 通过id获取数据
	 */
	@Override
	public MemberGrade findById(MemberGrade memberGrade) {
		return memberGradeMapper.selectMemberGradeById(memberGrade.getId());
	}
	/**
	 * 编辑会员等级信息
	 */
	
	@Override
	public void editMemberGrade(MemberGrade memberGrades) {
		if(memberGrades.getCanDevelopeMember()==null){
			memberGrades.setCanDevelopeMember(false);
		}
		if(memberGrades.getWalletCanConsume()==null){
			memberGrades.setWalletCanConsume(false);
		}
		if(memberGrades.getWalletCanReceive()==null){
			memberGrades.setWalletCanReceive(false);
		}
		if(memberGrades.getWalletCanRecharge()==null){
			memberGrades.setWalletCanRecharge(false);
		}
		if(memberGrades.getWalletCanTransfer()==null){
			memberGrades.setWalletCanTransfer(false);
		}
		if(memberGrades.getNeedAudit()==null){
			memberGrades.setNeedAudit(false);
		}
		memberGradeMapper.updateByPrimaryKey(memberGrades);
		
	}

	/**
	 * 删除会员等级信息
	 */
//	@Override
//	public void deleteMemberGrade(MemberGrade memberGrade) {
//		memberGradeMapper.deleteByPrimaryKey(memberGrade.getId()); 
//	
//	}

	/**
	 * 查看会员等级详情信息
	 */
	@Override
	public MemberGrade queryMemberGradeById(MemberGrade memberGrade) {
		return memberGradeMapper.queryMemberGradeById(memberGrade.getId());
	}

	@Override
	public List<Map> getAllMemberGradeIdAndName(Long groupId) {
		return memberGradeMapper.getAllMemberGradeIdAndName(groupId);
	}


	/**
	 * 获取会员等级名称列表
	 */
	@Override
	public List<MemberGrade> getGradeList(MemberInDTO memberInDTO) {
		List<MemberGrade> memberGradeList=memberGradeMapper.findMemberGradeList(memberInDTO);
		return memberGradeList;
	}

	
	/**
	 * 会员列表等级下拉列表
	 */
	@Override
	public List<MemberGrade> queryGradeList(MemberInDTO memberInDTO) {
		List<MemberGrade> memberGradeList=memberGradeMapper.queryMemberGradeList(memberInDTO);
		return memberGradeList;
	}
	
	//客户关系会员等级下拉
	@Override
	public List<MemberGrade> getShopGradeList(ShopUserInDTO shopUserInDTO) {
		List<MemberGrade> memberGradeList=memberGradeMapper.queryShopMemberGradeList(shopUserInDTO);
		return memberGradeList;
	}



	@Override
	public int deleteByPrimaryKey(Long id) {
		return 0;
	}

	@Override
	public int insert(MemberGrade record) {
		return 0;
	}

	@Override
	public MemberGrade selectByPrimaryKey(Long id) {
		return null;
	}

	@Override
	public List<MemberGrade> selectAll() {
		return null;
	}

	@Override
	public int updateByPrimaryKey(MemberGrade record) {
		return 0;
	}

	@Override
	public MemberGradeOutDTO findAmountByUpdateAmount(MemberGradeInDTO memberOut) {
		return memberGradeMapper.findAmountByUpdateAmount(memberOut);
	}

	public MemberGradeOutDTO findAmountByUpdateAmount2(MemberGradeInDTO memberOut) {
		return memberGradeMapper.findAmountByUpdateAmount2(memberOut);
	}

	@Override
	public List<MemberFee> queryFees(MemberGradeInDTO memberGradeOut) {
		return memberFeeMapper.queryFees(memberGradeOut);
	}

	@Override
	public MemberGrade queryByUpdateAmount(Long id) throws Exception {
		return memberGradeMapper.queryByUpdateAmount(id);
	}
	@Override
	public void memberRigthCheck(Long gradeId,BizEnum.MemberRigth memberRigth) throws Exception{

		if(memberGradeMapper.selectMemberRightCount(gradeId,memberRigth.getType()) == 0){
            throw new ApplicationException(memberRigth.getName() + ":不能");
		}
	}

	@Override
	public List<MemberGrade> memberGrades(MemberGradeInDTO memberGradeInDTO) {
		return memberGradeMapper.selectGroupList(memberGradeInDTO.getGroupId());
	}
}
